function buy() {
    $('#followDialog').show();
    $('#mask').show();
}

const $confirmBtn = $('#confirmBtn');
const $tzmoney = $('#tzmoney');
const $quickBtns = $('.quick-amount-btn');
// 关闭弹窗
$('#closeDialog, #mask').click(function() {
    $('#followDialog').hide();
    $('#mask').hide();
});

$(".amount-unit-select").change();
// 时间选择


// 快捷金额按钮 - 横向滚动选择
$quickBtns.click(function() {
     amount = $(this).data('amount');
    $tzmoney.val(amount);
    $("#expectedProfit").text(amount);
    $quickBtns.removeClass('selected');
    $(this).addClass('selected');

    // 自动滚动到选中的按钮
    $(this)[0].scrollIntoView({ behavior: 'smooth', block: 'nearest', inline: 'center' });
    toggleConfirmButton();
});

// 手动输入金额时，取消所有快捷按钮的选中状态
$tzmoney.on('input', function() {
    this.value = this.value
        .replace(/[^\d.]/g, '')
        .replace(/(\.\d{2})\d+/, '$1');
    $quickBtns.removeClass('selected');
    amount = this.value;
    $("#expectedProfit").text(amount);
    toggleConfirmButton();
});

// 确认按钮状态切换
function toggleConfirmButton() {
    const value = $tzmoney.val();
    if (value && !isNaN(value) && parseFloat(value) > 0) {
        $confirmBtn.removeAttr('disabled');
    } else {
        $confirmBtn.attr('disabled', 'disabled');
    }
}

function changeWallet(obj) {
     coin_id = $(obj).val();
     $.post(wallet_url,{coin_id:coin_id},function (res){
       if (res.code>0) {
           $.toptip(res.msg, 'error');
           return;
       } else {
           wallet_amount = res.data.account_amount;
           $(".account").text(wallet_amount +" "+res.data.code);
       }
     },'json')
}

function rise(r,obj) {
    $(obj).parent().find(".direction-btn").removeClass("active");
    $(obj).addClass("active");
    is_rise = r;
}
// 确认按钮点击事件
$confirmBtn.click(function() {
    if (amount>wallet_amount) {
        $.toptip("钱包余额不足", 'error');
        return;
    }
    if (amount<=0) {
        $.toptip("请输入正确的金额", 'error');
        return;
    }
    $.showLoading('提交中');
    $.post("/trade/create",{amount:amount,coin_id:coin_id,code:code,is_rise:is_rise,market_id:market_id},function (res){
        $.hideLoading();
         if (res.code==0) {
             $.toast(`跟单成功！`, function (){
                 window.location.reload();
             },'success');
         } else {
             $.toptip(res.msg, 'error');
         }
    },'json')
});
